package com.wpzf.zxhc.service.impl;

import com.wpzf.zxhc.utils.MultipleFileUnZip;
import com.wpzf.zxhc.domain.FeatureSTDao;
import com.wpzf.zxhc.service.DataProcessService;
import org.gdal.gdal.gdal;
import org.gdal.ogr.*;
import org.gdal.osr.SpatialReference;
import org.springframework.stereotype.Service;

import java.io.File;
import java.util.*;

@Service
public class DataProcessServiceImpl implements DataProcessService {


    MultipleFileUnZip multipleFileUnZip = new MultipleFileUnZip();
    @Override
    public Map<String, Object>   shptowkt(String sFilePath) {
        Map<String, Object> map=new HashMap<>();
        List<FeatureSTDao> models=new ArrayList<FeatureSTDao>();
        // 为了支持中文路径，请添加下面这句代码
        gdal.SetConfigOption("GDAL_FILENAME_IS_UTF8", "YES");
        // 为了使属性表字段支持中文，请添加下面这句
        gdal.SetConfigOption("SHAPE_ENCODING", "UTF-8");
        try {
            //步骤1：解压获取shp
            File file = new File(sFilePath);
            String name = file.getName();
            String dirName = file.getName();
            if (name.lastIndexOf(".") > 0) {
                dirName = name.substring(0, name.lastIndexOf("."));
            }
            //输出路径
            String outPath = file.getParent() + "\\" + dirName;
            File folder = new File(outPath);
            // 检查文件夹是否存在
            if (folder.exists() && folder.isDirectory()) {
                // 文件夹存在，删除文件夹
                deleteDirectory(folder);
            }
            Random random = new Random();
            //解压
            Boolean flag = multipleFileUnZip.fileUnZip(sFilePath, outPath);
            if (!flag)
            {
                map.put("res","解压失败");
                map.put("data",models);
                return map;
            }
            File[] files = new File(outPath).listFiles();
            String shpfilePath = "";
            if (files.length > 0) {
                for (int i = 0; i < files.length; i++) {
                    File file2 = files[i];
                    if (file2.getName().endsWith("shp")) {
                        shpfilePath = file2.getPath();
                    }
                }
                System.out.println("shp" + shpfilePath);
            }
            if (shpfilePath.isEmpty())
            {
                map.put("res","所上传数据无矢量文件");
                map.put("data",models);
                return map;
            }
            else
            {

                //步骤1：解析shp文件 坐标系
                DataSource dataSource = ogr.Open(shpfilePath, 0);
                Layer layer = dataSource.GetLayer(0); //获取图层
                double[] layerExtent = layer.GetExtent();
                System.out.println("图层范围：minx:" + layerExtent[0] + ",maxx:" + layerExtent[1] + ",miny:" + layerExtent[2] + ",maxy:" + layerExtent[3]);
                //步骤2：循环要素 返回 wkt
                for (int i = 0; i < layer.GetFeatureCount(); i++)
                {
                    FeatureSTDao model=new FeatureSTDao();
                    Feature feature = layer.GetFeature(i);
                    Geometry geometry = feature.GetGeometryRef();
                    model.setWkt(geometry.ExportToWkt());
                    model.setCentX(geometry.Centroid().GetX());
                    model.setCentY(geometry.Centroid().GetY());
                    models.add(model);
                }
                map.put("res","");
                map.put("data",models);
                return map;
            }
        }
        catch (Exception ex)
        {
            map.put("res","");
            map.put("data",models);
            return map;
        }
    }
    public static boolean deleteDirectory(File directoryToBeDeleted) {
        File[] allContents = directoryToBeDeleted.listFiles();
        if (allContents != null) {
            for (File file : allContents) {
                deleteDirectory(file);
            }
        }
        return directoryToBeDeleted.delete();
    }
}
